package org.williamjoy.gexpense;

import java.util.Calendar;
import java.util.TimeZone;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class EditExpenseActivity extends AbstractExpenseActivity {

    private long eventID;
    private EditText moneyView;
    private EditText titleView;
    private EditText locationView;
    private Spinner categoryView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        fillInForm();
        notifyDateDisplayRefresh();
    }

    protected void fillInForm() {
        Intent i = getIntent();
        this.eventID = i.getLongExtra(CalendarContract.Events._ID, -1);
        // if eventID==-1
        moneyView = (EditText) this.findViewById(R.id.editTextMoney);
        titleView = (EditText) this.findViewById(R.id.editTextTitle);
        locationView = (EditText) this.findViewById(R.id.editTextLocation);
        categoryView = (Spinner) this.findViewById(R.id.category_spinner);
        ArrayAdapter<String> adapter = (ArrayAdapter<String>) categoryView.getAdapter();
        int position = adapter.getPosition(i.getStringExtra(GExpenseConstants.ExpenseEvents.CATEGORY));
        categoryView.setSelection(position, true);
        moneyView.setText(String.valueOf(i.getDoubleExtra(
                GExpenseConstants.ExpenseEvents.MONEY, 0.0)));
        titleView.setText(i.getStringExtra(CalendarContract.Events.TITLE));
        locationView.setText(i
                .getStringExtra(CalendarContract.Events.EVENT_LOCATION));
        mDate.setTimeInMillis(i.getLongExtra(CalendarContract.Events.DTSTART,
                1L));
    }

    public void doSubmit() {
        Calendar beginTime = mDate;
        Calendar endTime = Calendar.getInstance();
        endTime = (Calendar) beginTime.clone();
        endTime.add(Calendar.HOUR_OF_DAY, 1);
        String strCalTitle = titleView.getText() + "|" + moneyView.getText();
        String strLocation = locationView.getText().toString();

        String category_name = categoryView.getSelectedItem().toString();
        Spinner spinner = (Spinner) this.findViewById(R.id.payfrom_spinner);
        String payFrom = spinner.getSelectedItem().toString();
        if (category_name == null || category_name.equals("Expense Type")) {

            Toast.makeText(getApplicationContext(),
                    "Please Choose an Expense Category!", Toast.LENGTH_SHORT)
                    .show();
            return;
        }
        if (titleView.getText().toString().equals("")) {

            Toast.makeText(getApplicationContext(),
                    "Please fill in Title field!", Toast.LENGTH_SHORT).show();
            return;
        }
        if (moneyView.getText().toString().equals("")) {

            Toast.makeText(getApplicationContext(),
                    "Please fill in Money field!", Toast.LENGTH_SHORT).show();
            return;
        }

        String properties = "#auto generated by GExpense App, http://goo.gl/0Xv4A";
        properties += "\nversion=1.0";
        properties += "\nexpense.title=" + titleView.getText();
        properties += "\nexpense.money=" + moneyView.getText();
        properties += "\nexpense.category=" + category_name;
        properties += "\nexpense.payfrom=" + payFrom;
        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues();
        values.put(Events.DTSTART, beginTime.getTimeInMillis());
        values.put(Events.DTEND, endTime.getTimeInMillis());

        values.put(Events.TITLE, strCalTitle);
        values.put(Events.DESCRIPTION, properties);
        values.put(Events.HAS_ALARM, false);
        values.put(Events.EVENT_LOCATION, strLocation);
        values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

        Uri uri = ContentUris.withAppendedId(Events.CONTENT_URI, eventID);
        cr.update(uri, values, null, null);

        Toast toast = Toast.makeText(getApplicationContext(),
                "Updated for you, I love Mary!", Toast.LENGTH_SHORT);
        toast.show();
        Intent resultIntent = new Intent();
        setResult(Activity.RESULT_OK, resultIntent);
        this.finish();
    }
}
